package cn.yunhe.service;

import cn.yunhe.dao.IAdminDao;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.User;
import cn.yunhe.util.SqlSessionFactoryUtil;
import net.sf.json.JSONArray;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

// 管理员的Service层
public class IAdminService {

    // 管理员登陆
    public Admin Lading(Admin admin) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            return iAdminDao.Lading(admin);
        } finally {
            session.close();
        }


    }

    // 用户全部信息
    public List<User> userlist() {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            return iAdminDao.Userlist();
        } finally {
            session.close();
        }

    }

    // 通过关键字查询用户的
    public JSONArray userlistName(String userName) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            List<User> list = iAdminDao.UserlistName(userName);
            JSONArray jsonArray = new JSONArray();
            jsonArray.addAll(list);
            return jsonArray;
        } finally {
            session.close();
        }

    }

    //通过id删除用户信息
    public boolean delUser(int userId) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            return iAdminDao.delUser(userId);
        } finally {
            session.commit();
            session.close();
        }

    }

    /**
     * 用户权限
     *
     * @param userId
     * @return
     */
    public boolean upUser(int userId) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            User user = iAdminDao.setFlaguser(userId);
            int flag = user.getFlag();
            int id = user.getUserId();
            if (flag == 0) {
                flag = 1;
            } else if (flag == 1) {
                flag = 0;
            }
            User user1 = new User();
            user1.setFlag(flag);
            user1.setUserId(id);
            int num = iAdminDao.upUser(user1);
            if (num != 0) {
                return true;
            } else {
                return false;
            }
        } finally {
            session.commit();
            session.close();
        }
    }

    // 查看用户的条数
    public int numuser() {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            return iAdminDao.numuser();
        } finally {
            session.close();
        }

    }

    /**
     * 通过ID查看个人用户
     *
     * @param id
     * @return
     */
    public User oneuser(int id) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            return iAdminDao.setFlaguser(id);
        } finally {
            session.close();
        }


    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    public boolean upAllUser(User user) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        try {
            int flag = iAdminDao.upUserAll(user);
            if (flag>0){
                session.commit();
                return true;
            }else {
                return false;
            }
        } finally {
            session.close();
        }
    }

    /**
     * 邓永凯
     * 所有的管理员
     *
     * @return
     */
    public List<Admin> getAllAdmin() {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IAdminDao iAdminDao = session.getMapper(IAdminDao.class);
        return iAdminDao.getAllAdmin();
    }
}
